import Vue 			from 'vue'
import util 		from '../../util'
import axios 		from 'axios'
import { Toast,Dialog } 	from 'vant';
Vue.use(Toast,Dialog);

export default {
	name: 'redDetails',
	data(){
		return {
			rid:'',
			type:'',
			info:{},
			coinType:'',

			url:'',					//发红包人的头像地址
			name:'',				//发红包的人用户名
			currency_id:'',			//红包币种
			errMsg:'',				//错误信息
			user_amount:'',			//当前用户领取的红包数量
			total:'',				//红包总数量
			get_nums:'',			//已被领取红包数量
			second:0,				//红包被领完用时
			list:[],
			uid:'',
		}
	},
	computed:{
		shownSecond(){
			//根据描述判断显示文字内容
			let time = this.second;
		    const newTime = parseInt(time);
		    if (newTime < 60) {
		    	return this.$t('redetails-key13',{time:newTime})
		    }
		    if (newTime < 3600) {
		    	return this.$t('redetails-key14',{time:parseInt(newTime / 60)})
		    }
		    return this.$t('redetails-key15',{time:parseInt(newTime / 3600)})
		},
		packetType(){
			if(this.type == 2){
				return this.$t('redetails-key6')
			}else if(this.type == 3){
				return this.$t('redetails-key7')
			}else if(this.type == 4){
				return this.$t('redetails-key8')
			}else if(this.type == 5){
				return this.$t('redetails-key9')
			}else if(this.type == 6){
				return this.$t('redetails-key10')
			}else if(this.type == 7){
				return this.$t('redetails-key11')
			}else if(this.type == 8){
				return this.$t('redetails-key12')
			}else{
				return ''
			}
		},
	},
	methods:{
		init(){
			util.fetch2(`${util.ajaxUrl}/red-packet/packet-list`,{
				id:this.rid,
				type:this.type
			}).then(res => {
				Toast.clear();
				if(res.retCode === 0){
					this.url = res.retData.url;
					this.name = res.retData.name;
					this.uid = res.retData.uid;
					this.currency_id = res.retData.currency_id;
					this.user_amount = Number(res.retData.user_amount);
					this.list = res.retData.list;
					this.total = Number(res.retData.total);
					this.second = Number(res.retData.second);
					this.get_nums = res.retData.get_nums;
					this.list.map(item => {
						item.time = util.transTime3(item.create_time)
					})
					this.list.reverse();
					//防止后台异步拿不到数据，轮询请求
					if(this.list.length < this.total && this.user_amount === 0){
						setTimeout(()=>{
							this.init();
						},300)
					}
				}else{
					this.errMsg = res.retMsg;
					Toast(this.errMsg)
				}
				if(Number(this.user_amount) <= 0) this.errMsg = this.$t('redetails-key5');
				// console.log(this.errMsg)
			}).catch(error => {
				console.log(error)
			})
		},
		goBack(){
			util.navigateBack(-1,this)
		}
	},
	created() {
		let _query = this.$route.query;
		if(_query){
			this.rid = _query.rid;
			this.type = _query.type;
			try{
				if(_query.info) this.info = JSON.parse(decodeURIComponent(_query.info));
				// console.log(this.info)
			}catch(e){
				Toast(this.$t('dataErr'));
			}
		}
		Toast.loading({
			message: this.$t('loading'),
			duration:0,
			forbidClick: true
		});
		util.waitSDKReady().then(token => {
			this.token = token;
			this.init();
		}).catch((e) => {
			Toast(this.$t('getTokenFail'))
		})
		setTimeout(() => {
			util.registerHandler('refreshCommonRequestWhenGoBackView',res => {
				if(this.token) this.init();
				util.callHandler('setStatusBarStyle',{
					style:'light'
				})
			})
			util.callHandler('setStatusBarStyle',{
				style:'light'
			})
		},300)
    }
}